package com.project.servlet;

import javax.servlet.http.HttpServletRequest;

import com.project.bean.User;
import com.project.bo.UserBO;
import com.project.enums.ServletResult;

public class LoginServlet extends AbstractServlet {
	
	private static final long serialVersionUID = 192398384729092931L;	
	
	@Override
	protected String validate(HttpServletRequest req) {
		
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		
		String result = ServletResult.NOT_USER.toString() ;
		
		User user = new User(username, password);
		
		UserBO userBO = UserBO.getInstance();
		User appUser = userBO.getUserByName(user);
		
		if (appUser != null) {
			if (!password.equalsIgnoreCase(appUser.getUserPassword())) {
				result = ServletResult.INVALID_PASSWORD.toString();
			} else {
				result = ServletResult.SUCCESS.toString();
			}
		}
		
		req.getSession().setAttribute("user", appUser);
		
		return result.trim();
	}

	@Override
	protected String insert(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String disable(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String update(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String enable(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String logout(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		
		return ServletResult.SUCCESS.toString();
	}
	
}
